﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Business.Fin;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.WinUI.Finance
{
    public partial class frmManageFinanceAccountsCode : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FinAccountsEntity mSelectedAccount;
        #endregion

        #region Constructor
        public frmManageFinanceAccountsCode()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindCombobox()
        {
            DataTable dt = FinAccountsBs.GetAccountsType();
            if (dt != null)
            {
                bool hasEmptyRow = false;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["ACCOUNTS_TYPE"] == null || dr["ACCOUNTS_TYPE"].ToString() == "")
                    {
                        hasEmptyRow = true;
                        break;
                    }
                }
                if (!hasEmptyRow)
                {
                    DataRow row = dt.NewRow();
                    row["ACCOUNTS_TYPE"] = "";
                    dt.Rows.InsertAt(row, 0);
                    dt.AcceptChanges();
                }
                cmbAccountType.DataSource = dt;
                cmbAccountType.DisplayMember = "ACCOUNTS_TYPE";
                cmbAccountType.ValueMember = "ACCOUNTS_TYPE";
            }
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtAccountCode.Text) == false)
            {
                condition += " AND A.ACCOUNTS_CODE LIKE '%" + txtAccountCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtAccountName.Text) == false)
            {
                condition += " AND A.ACCOUNTS_NAME LIKE '%" + txtAccountName.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtParentAccount.Text) == false && txtParentAccount.Tag != null)
            {
                FinAccountsEntity parent = (FinAccountsEntity)txtParentAccount.Tag;
                condition += " AND A.PARENT_CODE = '" + parent.AccountsCode + "'";
            }
            if (string.IsNullOrEmpty(cmbAccountType.Text) == false && cmbAccountType.SelectedValue != null)
            {
                condition += " AND A.ACCOUNTS_TYPE = '" + cmbAccountType.SelectedValue.ToString() + "'";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.ACCOUNTS_CODE";
            DataTable dt = FinAccountsBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAccount = new FinAccountsEntity(dr);
        }

        protected override void Click_View()
        {
            if (mSelectedAccount == null)
            {
                MessageBox.Show("请选择一个会计科目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainFinanceAccountsCode frm = new frmMaintainFinanceAccountsCode();
            frm.ThisActionType = ActionType.View;
            frm.SelectedAccount = mSelectedAccount;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedAccount == null)
            {
                MessageBox.Show("请选择一个会计科目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中会计科目？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FinAccountsBs.DeleteFinAccounts(mSelectedAccount);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n此会计科目可能已经被使用过，请联系管理员进行删除！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedAccount == null)
            {
                MessageBox.Show("请选择一个会计科目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainFinanceAccountsCode frm = new frmMaintainFinanceAccountsCode();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedAccount = mSelectedAccount;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainFinanceAccountsCode frm = new frmMaintainFinanceAccountsCode();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtParentAccount_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainFinanceAccountsCodeQuery frm = new frmMaintainFinanceAccountsCodeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAccount != null)
            {
                txtParentAccount.Text = frm.SelectedAccount.AccountsName;
                txtParentAccount.Tag = frm.SelectedAccount;
            }
            else
            {
                txtParentAccount.Text = "";
                txtParentAccount.Tag = null;
            }
        }
        #endregion

    }
}
